﻿Imports EIBWatcherNet.Core.EIB.Interfaces
Imports EIBWatcherNet.Core.EIB.Bus.Addressing

Namespace EIB.Managers
    Public NotInheritable Class EIBGroupAddressListManager
        Implements IEIBListManager(Of EIBGroupAddress)

        Private _addresses As List(Of UInt16) = New List(Of UInt16)


        Public Sub AddValue(address As EIBGroupAddress) Implements IEIBListManager(Of EIBGroupAddress).AddValue
            If Not _addresses.Contains(address.ToUint16) Then _addresses.Add(address.ToUint16)
        End Sub

        Private Sub AddValue(address As UInt16)
            If Not _addresses.Contains(address) Then _addresses.Add(address)
        End Sub

        Public Sub AddRange(addresses As IEnumerable(Of EIBGroupAddress)) Implements IEIBListManager(Of EIBGroupAddress).AddRange
            For Each value As Bus.Addressing.EIBGroupAddress In addresses
                AddValue(value.ToUint16)
            Next
        End Sub

        Public Sub Clear() Implements IEIBListManager(Of EIBGroupAddress).Clear
            _addresses.Clear()
        End Sub

        Public Function Contains(address As EIBGroupAddress) As Boolean Implements IEIBListManager(Of EIBGroupAddress).Contains
            Return _addresses.Contains(address.ToUint16)
        End Function

        Public Function GetValues() As List(Of EIBGroupAddress) Implements IEIBListManager(Of EIBGroupAddress).GetValues
            Dim outList As New List(Of EIBGroupAddress)

            For Each value As UInt16 In _addresses
                outList.Add(New EIBGroupAddress(value))
            Next

            Return outList
        End Function

        Public Sub Remove(address As EIBGroupAddress) Implements IEIBListManager(Of EIBGroupAddress).Remove
            If _addresses.Contains(address.ToUint16) Then _addresses.Remove(address.ToUint16)
        End Sub
    End Class
End Namespace